const fs = require('fs');
const packageJsonPath = './package.json';
// 读取package.json文件内容
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
const packageJson = JSON.parse(packageJsonContent);
if (process.env.VUE_APP_VERSION != packageJson.version) {
    fs.unlink(`./public/logo${packageJson.version}.ico`, function (err) { if (err) { throw err; } console.log(`文件:logo${packageJson.version}.ico删除成功！`); })
    fs.unlink(`./public/logo${packageJson.version}.png`, function (err) { if (err) { throw err; } console.log(`文件:logo${packageJson.version}.png删除成功！`); })
    fs.unlink(`./src/assets/logo${packageJson.version}.png`, function (err) { if (err) { throw err; } console.log(`文件:logo${packageJson.version}.png删除成功！`); })
}
const icoLogoPath = `./icon/${process.env.VUE_APP_NAME}.ico`;
const pngLogoPath = `./icon/${process.env.VUE_APP_NAME}.png`;

// 设置 json 中的字段
packageJson.name = process.env.VUE_APP_NAME;
packageJson.version = process.env.VUE_APP_VERSION;
packageJson.description = process.env.VUE_APP_DESCRIPTION;
const ico = fs.readFileSync(icoLogoPath); // 根据打包配置获取指定ico文件
const png = fs.readFileSync(pngLogoPath); // 根据打包配置获取指定png文件
fs.writeFileSync(`./public/logo${process.env.VUE_APP_VERSION}.ico`, ico);// 指定名字后写入目录
fs.writeFileSync(`./public/logo${process.env.VUE_APP_VERSION}.png`, png);
fs.writeFileSync(`./src/assets/logo${process.env.VUE_APP_VERSION}.png`, png);
// 将更新后的package.json写回文件
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');